//
//  TopBannersView.m
//  BProject
//
//  Created by 吴涛 on 15/8/17.
//  Copyright (c) 2015年 久远的回忆. All rights reserved.
//

#import "TopBannersView.h"
#import "UIImageView+WebCache.h"
@interface TopBannersView()<UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *mainScroll;
@property (nonatomic, strong)UIPageControl *page;

@end


@implementation TopBannersView


- (void)setModelArr:(NSMutableArray *)modelArr
{
    _modelArr = modelArr;
    for (int i = 0; i < modelArr.count; i ++) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0 + i * SCREEN_WIDTH, 0, SCREEN_WIDTH, _mainScroll.frame.size.height)];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:((HomeModel *)modelArr[i]).image]];
        
        [_mainScroll addSubview:imageView];
    }
    _mainScroll.contentSize = CGSizeMake(SCREEN_WIDTH * modelArr.count, self.frame.size.height);
    _page.numberOfPages = modelArr.count;
    _page.currentPage = 0;
    NSTimer *time = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(doScroll) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:time forMode:NSDefaultRunLoopMode];
    
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _mainScroll = [[UIScrollView alloc]initWithFrame:self.bounds];
        _mainScroll.pagingEnabled = YES;
        _mainScroll.showsVerticalScrollIndicator = NO;
        _mainScroll.showsHorizontalScrollIndicator = NO;
        _mainScroll.delegate = self;
        [self addSubview:_mainScroll];
        
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width / 2, self.frame.size.height / 5 * 4, self.frame.size.width / 2, 20)];
        _page.userInteractionEnabled = NO;
        [self addSubview:_page];
    }
    return self;
}

- (void)doScroll
{
    NSInteger count = _page.currentPage;
    _mainScroll.contentOffset = CGPointMake((count + 1) * SCREEN_WIDTH, 0);
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _page.currentPage = scrollView.contentOffset.x / SCREEN_WIDTH;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
